# cihash.rb - Case Insensitive Hash class
# Copyright (C) 2004-2009 Akira TAGOH

# Authors:
#   Akira TAGOH  <akira@tagoh.org>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

require 'delegate'


module PRUNE

=begin rdoc

== PRUNE::CiHash

=end

  class CiHash < DelegateClass(Hash)

=begin rdoc

=== PRUNE::CiHash#new(default = nil)

=end

    def initialize(default = nil)
      @keymaps = {}

      h = super({})
      h.default = default
      h
    end # def initialize

=begin rdoc

=== PRUNE::CiHash#[](key)

=end

    def [](key)
      __getobj__[(key.downcase rescue key)]
    end # def []

=begin rdoc

=== PRUNE::CiHash#[]=(key, val)

=end

    def []=(key, val)
      newkey = (key.downcase rescue key)
      @keymaps[newkey] = key unless @keymaps.has_key?(newkey)
      __getobj__[newkey] = val
    end # def []=

=begin rdoc

=== PRUNE::CiHash#clear

=end

    def clear
      @keymaps.clear

      __getobj__.clear
    end # def clear

=begin rdoc

=== PRUNE::CiHash#delete(key)

=end

    def delete(key)
      __getobj__.delete((key.downcase rescue key))
    end # def delete

=begin rdoc

=== PRUNE::CiHash#fetch(key, *arg, &block)

=end

    def fetch(key, *arg, &block)
      __getobj__.fetch((key.downcase rescue key), *arg, &block)
    end # def fetch

=begin rdoc

=== PRUNE::CiHash#values_at(*keys)

=end

    def values_at(*keys)
      newkeys = keys.map {|x| (x.downcase rescue x)}
      __getobj__.values_at(*newkeys)
    end # def values_at

    alias :indices :values_at
    alias :indexes :values_at

=begin rdoc

=== PRUNE::CiHash#has_key?(key)

=end

    def has_key?(key)
      __getobj__.has_key?((key.downcase rescue key))
    end # def has_key?

    alias :key? :has_key?
    alias :include? :has_key?
    alias :member? :has_key?

=begin rdoc

=== PRUNE::CiHash#to_origkey(key)

=end

    def to_origkey(key)
      @keymaps[(key.downcase rescue key)]
    end # def to_origkey

=begin rdoc

=== PRUNE::CiHash#each_origkey(&block)

=end

  def each_origkey(&block)
    @keymaps.each_value(&block)
  end # def each_origkey

=begin rdoc

=== PRUNE::CiHash#each_origpair(&block)

=end

  def each_origpair(&block)
    __getobj__.each_pair do |k,v|
      yield @keymaps[k], v
    end
  end # def each_origpair

  alias :orig_each :each_origpair

=begin rdoc

=== PRUNE::CiHash#origkeys

=end

  def origkeys
    __getobj__.keys.map {|x| @keymaps[x]}
  end # def origkeys

  end # class CiHash

end # module PRUNE
